package ar.com.sebasoft.rembrandt.adapter;

import java.util.List;

import org.holoeverywhere.widget.ArrayAdapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import ar.com.sebasoft.rembrandt.R;
import ar.com.sebasoft.rembrandt.model.Album;

import com.androidquery.AQuery;
import com.androidquery.callback.BitmapAjaxCallback;
import com.androidquery.util.Constants;

/**
 * @author SebaSOFT
 * 
 */
public class AlbumAdapter extends ArrayAdapter<Album> {

	private List<Album> mList;
	private static LruCache<Long, Bitmap> bMapCahce = new LruCache<Long, Bitmap>(100);

	public AlbumAdapter(Context context, List<Album> objects) {
		super(context, R.layout.li_album_item, objects);
		this.setNotifyOnChange(true);
		this.mList = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View row;
		LayoutInflater listInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if (null == convertView) {
			row = listInflater.inflate(R.layout.li_album_item, null);
		} else {
			row = convertView;
		}
		row.setTag(this.getItem(position));

		this.populateView(row, this.getItem(position));

		return row;
	}

	private void populateView(View row, Album item) {
		AQuery aq = new AQuery(row);

		aq.id(R.id.lblAlbumName).text(item.getNombre());
		aq.id(R.id.lblAlbumPhotos).text(R.string.msg_cnt_photos, item.getCantFotos());

		// thumbs
		if (item.getThumbPath1() != null) {
			// create a bitmap ajax callback object
			BitmapAjaxCallback cb = new BitmapAjaxCallback();

			// configure the callback
			cb.bitmap(this.getCachedThumbnail(item.getThumbPath1())).fallback(R.drawable.transparent).animation(Constants.FADE_IN);

			// invoke it with an image view
			aq.id(R.id.imgThumb1).image(cb);
		} else {
			aq.id(R.id.imgThumb1).image(R.drawable.transparent);
		}
		if (item.getThumbPath2() != null) {
			// create a bitmap ajax callback object
			BitmapAjaxCallback cb = new BitmapAjaxCallback();

			// configure the callback
			cb.bitmap(this.getCachedThumbnail(item.getThumbPath2())).fallback(R.drawable.transparent).animation(Constants.FADE_IN);

			// invoke it with an image view
			aq.id(R.id.imgThumb2).image(cb);
		} else {
			aq.id(R.id.imgThumb2).image(R.drawable.transparent);
		}
		if (item.getThumbPath3() != null) {
			// create a bitmap ajax callback object
			BitmapAjaxCallback cb = new BitmapAjaxCallback();

			// configure the callback
			cb.bitmap(this.getCachedThumbnail(item.getThumbPath3())).fallback(R.drawable.transparent).animation(Constants.FADE_IN);

			// invoke it with an image view
			aq.id(R.id.imgThumb3).image(cb);
		} else {
			aq.id(R.id.imgThumb3).image(R.drawable.transparent);
		}
	}

	private Bitmap getCachedThumbnail(Long imgID) {
		Bitmap res = null;

		if (imgID != null) {
			if (bMapCahce.get(imgID) != null) {
				res = bMapCahce.get(imgID);
			} else {
				res = MediaStore.Images.Thumbnails.getThumbnail(this.getContext().getContentResolver(), imgID, MediaStore.Images.Thumbnails.MICRO_KIND, null);
				if (res != null) {
					bMapCahce.put(imgID, res);
				}
			}
		}

		return res;
	}

	/**
	 * @return the mList
	 */
	public final List<Album> getmList() {
		return this.mList;
	}

}
